﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MobileWebsiteGenerator.Models;

namespace MobileWebsiteGenerator.Models
{
    public class GetDataFromDB
    {

        public static void GetAllData(int CustomerID)
        {
            Mobile_appContext mobileEntities = new Mobile_appContext();
            if (MvcApplication._MainCustomer.Website.web_id != 1)
            {
                var customer = mobileEntities.Customers.Where(cust => cust.Customer_id == CustomerID).FirstOrDefault();

                MvcApplication._MainCustomer.customer = customer;
                //Website
                var customerwebid = mobileEntities.CustomerWebsiteRels.Where(cw => cw.Customer_id == CustomerID).FirstOrDefault();
                var website = mobileEntities.Websites.Where(w => w.web_id == customerwebid.Web_id).FirstOrDefault();
                MvcApplication._MainCustomer.Website = website;
                //Theme
                if (MvcApplication._MainCustomer.Website != null)
                {
                    var theme = mobileEntities.ThemeDetails.Where(w => w.theme_id == mobileEntities.WebthemeRels.Where(wtr => wtr.Web_id == website.web_id).Select(t => t.Theme_id).FirstOrDefault()).FirstOrDefault();
                    MvcApplication._MainCustomer.MyTheme = theme;
                    //All Pages
                    var pagesId = mobileEntities.ThemePageRELs.Where(tpr => tpr.Theme_id == theme.theme_id).Select(p => p.Page_id);
                    var allpages = mobileEntities.Pages.Where(p => pagesId.Contains(p.page_id));
                    MvcApplication._MainCustomer.ContentPages = new List<Entities.Page>();
                    if (allpages != null && allpages.Count() > 0)
                    {
                        allpages.ToList().ForEach(ap =>
                        {
                            MvcApplication._MainCustomer.ContentPages.Add(ap);

                        });
                    }
                    try
                    {
                        //All pictures
                        var picsId = mobileEntities.ThemePictureRELs.Where(tpr => tpr.Theme_id == theme.theme_id).Select(p => p.Picture_id);
                        var allpics = mobileEntities.pictureGalleries.Where(p => picsId.Contains(p.picture_id));
                        MvcApplication._MainCustomer.PictureGallery = new List<Entities.pictureGallery>();
                        if (allpics != null && allpics.Count() > 0)
                        {
                            allpics.ToList().ForEach(ap =>
                            {
                                MvcApplication._MainCustomer.PictureGallery.Add(ap);

                            });
                        }

                    }
                    catch { }
                }
                else
                {
                    MvcApplication.showMessage = true;
                    MvcApplication.messageTxt = "please first Create the website !!";




                }

            }


        }
    }
}
